#include "stdafx.h"
#include "Starter.hpp"
#include <WinSock2.h>

bool HpsFramework::Startup::_started = false;

void HpsFramework::Startup::Clean()
{
	if(_started)
	{
		int err;
		if((err = WSACleanup()) != 0)
		{
			HPS_THROW(CleanupException, err);
		}
	}
}

void HpsFramework::Startup::Start()
{
	if(!_started)
	{
		int err;
		WSADATA data;
		if((err = WSAStartup(MAKEWORD(2,2), &data)) != 0)
		{
			HPS_THROW(StartupException, err);
			return;
		}
		else
		{
			if(LOBYTE(data.wVersion) != 2 || HIBYTE(data.wVersion) != 2)
			{
				HPS_THROW(StartupVersionException, 0);
				return;
			}
			_started = true;
		}
	}
}

HpsFramework::Startup::Startup()
{
	this->Start();
}

HpsFramework::Startup::~Startup()
{
	this->Clean();
}
